<template>
  <div class="enterprise-manage">
    <!-- 顶部操作区 -->
    <div class="operation-bar">
      <el-button type="primary" @click="handleAdd">
        <el-icon><Plus /></el-icon>添加用户
      </el-button>
    </div>

    <!-- 表格区域 -->
    <el-table 
      :data="tableData.slice((currentPage-1)*pageSize, currentPage*pageSize)" 
      style="width: 100%"
      :height="600"
      border>
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="username" label="用户名" />
      <el-table-column prop="realName" label="真实姓名" />
      <el-table-column prop="role" label="角色">
        <template #default="scope">
          <el-tag :type="scope.row.role === '管理员' ? 'danger' : 'primary'">
            {{ scope.row.role }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="registerTime" label="注册时间" width="180" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="scope">
          <el-tag :type="scope.row.status ? 'success' : 'info'">
            {{ scope.row.status ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="150">
        <template #default="scope">
          <el-button 
            :type="scope.row.status ? 'warning' : 'success'"
            :icon="scope.row.status ? Lock : Unlock"
            circle
            @click="handleActivate(scope.row)"
            :title="scope.row.status ? '禁用' : '激活'"
          />
          <el-button 
            type="danger" 
            :icon="Delete"
            circle
            @click="handleDelete(scope.row)"
            title="删除"
          />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 50]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
      />
    </div>

    <!-- 添加用户对话框 -->
    <el-dialog
      v-model="dialogVisible"
      title="添加用户"
      width="500px"
    >
      <el-form :model="form" label-width="100px" :rules="rules" ref="formRef">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名"/>
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名"/>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="form.role" placeholder="请选择角色" style="width: 100%">
            <el-option label="管理员" value="管理员" />
            <el-option label="普通用户" value="普通用户" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="form.status" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit(formRef)">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { Lock, Unlock, Delete, Plus } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';

// 分页相关
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);

// 模拟数据
const tableData = ref([
  {
    id: 1,
    username: 'admin',
    realName: '管理员',
    role: '管理员',
    registerTime: '2024-01-01 10:00:00',
    status: true
  },
  {
    id: 2,
    username: 'user1',
    realName: '张三',
    role: '普通用户',
    registerTime: '2024-01-02 14:30:00',
    status: true
  }
]);

// 计算总数
total.value = tableData.value.length;

// 表单校验规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  realName: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' }
  ],
  role: [
    { required: true, message: '请选择角色', trigger: 'change' }
  ]
};

// 对话框相关
const dialogVisible = ref(false);
const formRef = ref(null);
const form = ref({
  username: '',
  realName: '',
  role: '',
  status: true
});

// 添加激活/禁用函数
const handleActivate = (row) => {
  const action = row.status ? '禁用' : '激活';
  ElMessageBox.confirm(
    `确定要${action}该用户吗？`,
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      row.status = !row.status;
      ElMessage.success(`${action}成功`);
    })
    .catch(() => {
      ElMessage.info('已取消操作');
    });
};

// 删除用户
const handleDelete = (row) => {
  ElMessageBox.confirm(
    '确定要删除该用户吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      const index = tableData.value.findIndex(item => item.id === row.id);
      if (index !== -1) {
        tableData.value.splice(index, 1);
      }
      ElMessage.success('删除成功');
    })
    .catch(() => {
      ElMessage.info('已取消删除');
    });
};

// 添加用户
const handleAdd = () => {
  form.value = {
    username: '',
    realName: '',
    role: '',
    status: true
  };
  dialogVisible.value = true;
};

// 提交表单
const handleSubmit = async (formEl) => {
  if (!formEl) return;
  
  await formEl.validate((valid) => {
    if (valid) {
      // 添加逻辑
      const newUser = {
        id: tableData.value.length + 1,
        ...form.value,
        registerTime: new Date().toLocaleString()
      };
      tableData.value.push(newUser);
      ElMessage.success('添加成功');
      dialogVisible.value = false;
    }
  });
};
</script>

<style scoped>
.enterprise-manage {
  padding: 20px;
}

.operation-bar {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-button {
  margin-right: 10px;
}

:deep(.el-table) {
  margin-top: 20px;
}

:deep(.el-dialog__body) {
  padding: 20px;
}
</style>
